package com.testmanager.testmananger.shuiyou.szjk.web;


import com.alibaba.fastjson.JSONObject;



import com.testmanager.testmananger.shuiyou.szjk.domain.ZipTypeEntity;
import com.testmanager.testmananger.shuiyou.szjk.dto.ChangeExpressionDto;
import com.testmanager.testmananger.shuiyou.szjk.dto.ZipDto;
import com.testmanager.testmananger.shuiyou.szjk.dto.ZipFileDto;
import com.testmanager.testmananger.shuiyou.szjk.repository.ChangeRecordRepository;
import com.testmanager.testmananger.shuiyou.szjk.service.SendZipService;
import com.testmanager.testmananger.shuiyou.szjk.service.UnzipUploadService;
import com.testmanager.testmananger.shuiyou.szjk.service.ZipAndSendService;
import com.testmanager.testmananger.shuiyou.szjk.service.ZipTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import javax.validation.constraints.NotNull;
import java.util.List;


@RestController
@RequestMapping("/shuiyou/szjk")
@Slf4j
public class SZJKResource {

    @Autowired
    UnzipUploadService unzipUploadService;

    @Autowired
    ZipAndSendService zipAndSendService;

    @Autowired
    ZipTypeService zipTypeService;

    @Autowired
    SendZipService sendZipService;

    @Autowired
    ChangeRecordRepository changeRecordRepository;


    /**
     * 1.接收修改的数据内容，changerecord
     * 2.
     */
    @PostMapping("savechangerecord/{id}/{title}")
    public ResponseEntity saveChangeRecord(@PathVariable("id")long id,
                                           @PathVariable("title")String title,
                                           @RequestBody List<ChangeExpressionDto> list){
        log.info("visitor:savechangerecord/{}/{}",String.valueOf(id),title);
        return zipAndSendService.saveFileChangeExpressions(list,id,title);
    }


    /**
     * 上传并返回xml名称列表 及内容
     *
     * @param file
     * @return
     */
    @PostMapping("/upload")
    public ResponseEntity upload(@RequestParam("type")long typeid,@RequestParam(value = "file",required = false) MultipartFile file){



        return unzipUploadService.upload(typeid,file);

    }

    @PostMapping("/getFileContents")
    public List<ZipFileDto> getFileContents(@RequestBody ZipDto zipDto){
        return  unzipUploadService.getZipFiles(zipDto);
    }

    @PostMapping("/getjufanFileContents")
    public List<ZipFileDto> getjufanFileContents(@RequestBody ZipDto zipDto){
        return  unzipUploadService.getJuduanFiles(zipDto);
    }

    /**
     * 可能需要做分页
     * @return
     */
    @GetMapping ("/getzipnames")
    public List<ZipDto> getZipList(){
        return unzipUploadService.getZipDtos();
    }




   // public Map<String,String> getXmlFiles()

    /**
     * 1.接收并根据名称压缩
     * 2.发送到远程并获得响应报文
     * 3.返回报文
     * @param
     */
    @PostMapping("submit/{id}/{title}")
    public ResponseEntity submitAndSend(@PathVariable("id")long id,@PathVariable("title")String title,@RequestBody List<ZipFileDto> zipFileDtos){

            JSONObject zipname = zipAndSendService.zipFiles(id,title,zipFileDtos);
        return  ResponseEntity.ok(zipname);
    }


    @GetMapping("send")
    public ResponseEntity send2Remote(@RequestParam("typeid")@NotNull long id, @RequestParam("path")@NotNull String path){
       return  sendZipService.send(id,path);
    }


    @GetMapping("getziptypes")
    public ResponseEntity getziptype(){
        return zipTypeService.getZipTypes();
    }


    @PostMapping("saveziptype")
    public ResponseEntity  saveZiptype(@RequestBody ZipTypeEntity zipTypeEntity){
        return zipTypeService.saveZipType(zipTypeEntity);
    }

    @PostMapping("saveziptypes")
    public ResponseEntity saveziptypes(@RequestBody List<ZipTypeEntity> zipTypeEntities){
        return zipTypeService.saveZipTypes(zipTypeEntities);
    }


    /**
     * 临时
     */

    @GetMapping("getRecord")
    public ResponseEntity getAllRecord(){
        return ResponseEntity.ok(changeRecordRepository.findAll());
    }



    @PostMapping("saveRecord/{zipid}/{title}")
    public ResponseEntity saveRecord(@PathVariable("zipid")long zipid,@PathVariable("title")String title,@RequestBody List<ChangeExpressionDto> changeExpressionDtos){
        return zipAndSendService.saveFileChangeExpressions(changeExpressionDtos,zipid,title);
    }





}
